﻿var registerWindow;

function showRegisterForm() {
    if (!registerWindow) {
        var fieldName = new Ext.form.field.Text({
            xtype: 'textfield',
            allowBlank: false,
            blankText: '用户名不能为空',
            emptyText: '请输入用户名'
        })
        var registerForm = Ext.widget('form', {
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            border: false,
            bodyPadding: 10,

            fieldDefaults: {
                labelAlign: 'top',
                labelWidth: 100,
                labelStyle: 'font-weight:bold'
            },
            defaults: {
                margins: '0 0 10 0'
            },

            items: [
                    { xtype: 'fieldcontainer',
                        fieldLabel: '请填写邮箱地址',
                        combineErrors: false,
                        layout: 'hbox',
                        defaults: {
                            hideLabel: true
                        },
                        items: [fieldName, { xtype: 'displayfield',
                            value: '@abc.com'
                        }]
                    }],

            buttons: [{
                text: '取消',
                handler: function () {
                    registerForm.getForm().reset();
                    registerWindow.close();
                }
            }, {
                text: '提交',
                handler: function () {
                    if (registerForm.getForm().isValid()) {
                        // In a real application, this would submit the form to the configured url
                        registerForm.getForm().submit({
                            url: '/User/Register',
                            params: { UserName: fieldName.getValue() },
                            waitMsg: '提交中...',
                            success: function (form, action) {
                                Ext.Msg.alert('提示', '提交成功', function () { registerForm.getForm().reset();  accordionPanel.setTitle('欢迎 ' + action.result.msg); });
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('提示', '提交失败', function () { registerForm.getForm().reset(); });
                            }
                        });
                        //registerForm.getForm().reset();
                        registerWindow.close();
                        //Ext.MessageBox.alert('Thank you!', 'Your inquiry has been sent. We will respond as soon as possible.');
                    }
                }
            }]
        });

        registerWindow = Ext.widget('window', {
            title: '用户注册',
            closeAction: 'hide',
            width: 400,
            height: 200,
            minHeight: 200,
            layout: 'fit',
            resizable: true,
            modal: true,
            items: registerForm
        });
    }
    registerWindow.show();
}

var loginWindow;

function showLoginForm() {
    if (!loginWindow) {
        var fieldName = new Ext.form.field.Text({
            xtype: 'textfield',
            allowBlank: false,
            fieldLabel: '用户名',
            blankText: '用户名不能为空',
            emptyText: '请输入用户名'
        })
        var fieldPass = new Ext.form.field.Text({
            xtype: 'textfield',
            inputType: 'password',
            allowBlank: false,
            fieldLabel: '密码',
            blankText: '密码不能为空',
            emptyText: '请输入密码',
            labelAlign: 'left',
            labelWidth: 100,
            labelStyle: 'font-weight:normal',
            margins: '0 0 0 0'
        })
        var loginForm = Ext.widget('form', {
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            border: false,
            bodyPadding: 10,

            fieldDefaults: {
                labelAlign: 'top',
                labelWidth: 100,
                labelStyle: 'font-weight:bold'
            },
            defaults: {
                margins: '0 0 10 0'
            },

            items: [
                    { xtype: 'fieldcontainer',
                        fieldLabel: '请填写用户名和密码',
                        combineErrors: false,
                        layout: 'hbox',
                        items: [fieldName, { xtype: 'displayfield',
                            value: '@abc.com'
                        }]
                    }, fieldPass],

            buttons: [{
                text: '取消',
                handler: function () {
                    loginForm.getForm().reset();
                    loginWindow.close();
                }
            }, {
                text: '登录',
                handler: function () {
                    if (loginForm.getForm().isValid()) {
                        // In a real application, this would submit the form to the configured url
                        loginForm.getForm().submit({
                            url: '/User/Login',
                            params: { UserName: fieldName.getValue(), UserPass: fieldPass.getValue() },
                            waitMsg: '提交中...',
                            success: function (form, action) {
                                Ext.Msg.alert('提示', '提交成功', function () { loginForm.getForm().reset(); accordionPanel.setTitle('欢迎 ' + action.result.msg); });
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('提示', '提交失败', function () { loginForm.getForm().reset(); });
                            }
                        });
                        //registerForm.getForm().reset();
                        loginWindow.close();
                        //Ext.MessageBox.alert('Thank you!', 'Your inquiry has been sent. We will respond as soon as possible.');
                    }
                }
            }]
        });

        loginWindow = Ext.widget('window', {
            title: '用户登录',
            closeAction: 'hide',
            width: 400,
            height: 250,
            minHeight: 250,
            layout: 'fit',
            resizable: true,
            modal: true,
            items: loginForm
        });
    }
    loginWindow.show();
}

var passmodifyWindow;

function showPassModifyForm() {
    if (!passmodifyWindow) {
        var fieldOldPass = new Ext.form.field.Text({
            xtype: 'textfield',
            inputType: 'password',
            allowBlank: false,
            fieldLabel: '原密码',
            blankText: '密码不能为空',
            emptyText: '请输入密码'
        });
        var fieldNewPass1 = new Ext.form.field.Text({
            xtype: 'textfield',
            name: 'password1',
            inputType: 'password',
            allowBlank: false,
            fieldLabel: '请输入密码',
            blankText: '密码不能为空',
            emptyText: '请输入密码'
        });
        var fieldNewPass2 = new Ext.form.field.Text({
            xtype: 'textfield',
            inputType: 'password',
            allowBlank: false,
            fieldLabel: '确认密码',
            blankText: '密码不能为空',
            emptyText: '请输入密码',
            validator: function (value) {
                var password1 = this.previousSibling('[name=password1]');
                return (value === password1.getValue()) ? true : '密码不一致'
            }
        });
        var passmodifyForm = Ext.widget('form', {
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            border: false,
            bodyPadding: 10,

            fieldDefaults: {
                labelAlign: 'top',
                labelWidth: 100,
                labelStyle: 'font-weight:bold'
            },
            defaults: {
                margins: '0 0 10 0'
            },

            items: [
                    { xtype: 'fieldcontainer',
                        fieldLabel: '请填写原密码，并输入新密码',
                        combineErrors: false,
                        items: [fieldOldPass, fieldNewPass1, fieldNewPass2]
                    }],

            buttons: [{
                text: '取消',
                handler: function () {
                    passmodifyForm.getForm().reset();
                    passmodifyWindow.close();
                }
            }, {
                text: '提交',
                handler: function () {
                    if (passmodifyForm.getForm().isValid()) {
                        // In a real application, this would submit the form to the configured url
                        passmodifyForm.getForm().submit({
                            url: '/User/ModifyUserPass',
                            params: { OldPass: fieldOldPass.getValue(), NewPass: fieldNewPass1.getValue() },
                            waitMsg: '提交中...',
                            success: function (form, action) {
                                Ext.Msg.alert('提示', '提交成功', function () { passmodifyForm.getForm().reset(); });
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('提示', '提交失败', function () { passmodifyForm.getForm().reset(); });
                            }
                        });
                        //registerForm.getForm().reset();
                        passmodifyWindow.close();
                        //Ext.MessageBox.alert('Thank you!', 'Your inquiry has been sent. We will respond as soon as possible.');
                    }
                }
            }]
        });

        passmodifyWindow = Ext.widget('window', {
            title: '修改密码',
            closeAction: 'hide',
            width: 400,
            height: 250,
            minHeight: 250,
            layout: 'fit',
            resizable: true,
            modal: true,
            items: passmodifyForm
        });
    }
    passmodifyWindow.show();
}

var passforgetWindow;

function showPassForgetForm() {
    if (!passforgetWindow) {
        var fieldName = new Ext.form.field.Text({
            xtype: 'textfield',
            allowBlank: false,
            fieldLabel: '用户名',
            blankText: '用户名不能为空',
            emptyText: '请输入用户名'
        });

        var passforgetForm = Ext.widget('form', {
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            border: false,
            bodyPadding: 10,

            fieldDefaults: {
                labelAlign: 'top',
                labelWidth: 100,
                labelStyle: 'font-weight:bold'
            },
            defaults: {
                margins: '0 0 10 0'
            },

            items: [
                    { xtype: 'fieldcontainer',
                        fieldLabel: '请填写用户名',
                        combineErrors: false,
                        layout: 'hbox',
                        items: [fieldName, { xtype: 'displayfield',
                            value: '@abc.com'
                        }]
                    }],

            buttons: [{
                text: '取消',
                handler: function () {
                    passforgetForm.getForm().reset();
                    passforgetWindow.close();
                }
            }, {
                text: '提交',
                handler: function () {
                    if (passforgetForm.getForm().isValid()) {
                        // In a real application, this would submit the form to the configured url
                        passforgetForm.getForm().submit({
                            url: '/User/ForgetUserPass',
                            params: { UserName: fieldName.getValue() },
                            waitMsg: '提交中...',
                            success: function (form, action) {
                                Ext.Msg.alert('提示', '提交成功', function () { passforgetForm.getForm().reset(); });
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('提示', '提交失败', function () { passforgetForm.getForm().reset(); });
                            }
                        });
                        //registerForm.getForm().reset();
                        passforgetWindow.close();
                        //Ext.MessageBox.alert('Thank you!', 'Your inquiry has been sent. We will respond as soon as possible.');
                    }
                }
            }]
        });

        passforgetWindow = Ext.widget('window', {
            title: '忘记密码',
            closeAction: 'hide',
            width: 400,
            height: 250,
            minHeight: 250,
            layout: 'fit',
            resizable: true,
            modal: true,
            items: passforgetForm
        });
    }
    passforgetWindow.show();
}